package cn.crawler.app.web.modules.controllers.platform.crown;

import cn.crawler.app.crown.modules.models.CrownEquipment;
import cn.crawler.app.crown.modules.services.CrownEquipmentService;
import cn.crawler.app.web.commons.ulog.annotation.SLog;
import cn.crawler.app.web.commons.utils.StringUtil;
import cn.crawler.framework.util.Result;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;


/**
 * 设备管理
 */
@IocBean
@At("/platform/crown/equipment")
public class CrownEquipmentController {

    @Reference
    @Inject
    private CrownEquipmentService equipmentService;

    @At("")
    @Ok("beetl:/platform/crown/equipment/index.html")
    @RequiresPermissions("crown.manager.equipment.list")
    public void index() {
    }


    /**
     * 查询设备列表（分页）
     *
     * @param searchKeyword String 条件搜索参数
     * @param searchDate    String[] 按搜索时间参数
     * @param pageNumber
     * @param pageSize
     * @return Object
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("crown.manager.equipment.list")
    public Object data(@Param("searchKeyword") String searchKeyword,
                       @Param("searchDate") String[] searchDate,
                       @Param("pageNumber") int pageNumber,
                       @Param("pageSize") int pageSize) {
        return Result.success().addData(equipmentService.data(searchKeyword, searchDate, pageNumber, pageSize));
    }


    /**
     * 添加账号
     *
     * @param equipment
     * @return
     */
    @At("/add")
    @Ok("json")
    @RequiresPermissions("crown.manager.equipment.add")
    @SLog(tag = "新建设备", msg = "设备名称:${args[0].name}")
    public Object add(@Param("..") CrownEquipment equipment) throws Exception {
        equipment.setOpBy(StringUtil.getPlatformUid());
        equipmentService.add(equipment);
        return Result.success();
    }


    /**
     * 根据id查询设备信息
     *
     * @param id 设备id
     * @return Object
     */
    @At("/edit/?")
    @Ok("json:full")
    public Object edit(@Param("id") String id) {
        return Result.success(equipmentService.fetch(id));
    }


    /**
     * 修改设备信息
     *
     * @param equipment
     * @return
     */
    @At("/editDo")
    @Ok("json")
    @RequiresPermissions("crown.manager.equipment.edit")
    @SLog(tag = "修改设备", msg = "设备名称:${args[1].getAttribute('name')}")
    public Object editDo(@Param("..") CrownEquipment equipment) {
        equipmentService.editDo(equipment);
        return Result.success();
    }


    /**
     * 根据id启用设备
     *
     * @param id
     * @return
     */
    @At("/enable/?")
    @Ok("json")
    @RequiresPermissions("crown.manager.equipment.edit")
    @SLog(tag = "启用设备", msg = "设备id:${args[1].getAttribute('id')}")
    public Object enable(String id) {
        equipmentService.clearCache();
        equipmentService.update(Chain.make("disabled", false), Cnd.where("id", "=", id));
        return Result.success();
    }

    /**
     * 根据id禁用设备
     *
     * @param id
     * @return
     */
    @At("/disable/?")
    @Ok("json")
    @RequiresPermissions("crown.manager.equipment.edit")
    @SLog(tag = "禁用设备", msg = "设备id:${args[1].getAttribute('id')}")
    public Object disable(String id) {
        equipmentService.clearCache();
        equipmentService.update(Chain.make("disabled", true), Cnd.where("id", "=", id));
        return Result.success();
    }

    /**
     * 根据id删除设备
     *
     * @param id
     * @return
     */
    @At("/delete/?")
    @Ok("json")
    @RequiresPermissions("crown.manager.equipment.delete")
    @SLog(tag = "删除设备", msg = "设备id:${args[1].getAttribute('id')}")
    public Object delete(String id) {
        equipmentService.clearCache();
        equipmentService.delete(id);
        return Result.success();
    }


}
